
<!-- 重新打印交班单-弹窗组件 -->
<template>
  <div></div>
</template>

<script setup lang="ts">
import { watch } from 'vue'
import ItMessageService from '@/components/messageTips/index'
import { printerReprint } from '@/api/common'
import { ElMessage } from 'element-plus'

interface Props {
  modelValue: boolean
  selectedHandoverOrder?: any
  selectCheckedList: any[]
}

const props = withDefaults(defineProps<Props>(), {
  modelValue: false,
  selectedHandoverOrder: null,
  selectCheckedList: () => []
})

const emit = defineEmits(['update:modelValue', 'confirm'])

// 监听弹窗显示状态
watch(
  () => props.modelValue,
  newVal => {
    if (newVal) {
      showPrintDialog()
    }
  }
)

// 显示打印确认弹窗
const showPrintDialog = async () => {
  if (!props.selectedHandoverOrder) {
    ElMessage.error('请先选择交班单')
    emit('update:modelValue', false)
    return
  }

  try {
    const messageInstance = await ItMessageService.message(
      {
        title: '重新打印交班单',
        content: `您确定重新打印交班单【${props.selectedHandoverOrder.shift_id}】吗？`,
        onSumbitText: '确定',
        onCancelText: '取消'
      },
      async () => {
        // 确认回调
        try {
          // 设置loading状态
          messageInstance.setLoading(true)

          const shift_id = props.selectCheckedList.map(item => item.value)
          const res = await printerReprint({
            ticket_id: props.selectedHandoverOrder.shift_id,
            check_list: shift_id
          })

          if (res.status === 200) {
            ElMessage.success('操作成功')
            emit('confirm')
            // 手动关闭弹窗
            messageInstance.close()
          }
        } catch (error) {
          console.error('打印失败:', error)
          ElMessage.error('打印失败')
          // 失败时移除loading状态，允许重试
          messageInstance.setLoading(false)
        } finally {
          messageInstance.setLoading(false)
        }
      }
    )
  } catch (error) {
    // 用户取消
    console.log('用户取消打印')
  } finally {
    emit('update:modelValue', false)
  }
}
</script>

<style lang="scss" scoped>
// 空样式，因为使用 ItMessageService
</style>
